import Vue from 'vue'
import VueRouter from 'vue-router'

import layout from '@/layout/layout'
import NProgress from "nprogress";
import "nprogress/nprogress.css";

import {getToken} from "@/utils/auth";
import {getCookie} from "@/utils/cookies";
import {getPermission} from "@/router/permission";
import store from "@/store";

Vue.use(VueRouter)

export let constantRouterMap = [{
  path: '/',
  component: () => import('@/view/login/login'),
  //自定义obj false代表不需要登录/权限就能访问 true代表需要登录/权限才能访问
  meta:{
    requireAuth:false
  },
  hidden: true
}, {
  path: '/',
  component: () => import('@/view/404/404'),
  meta:{
    requireAuth:false
  },
  hidden: true
}, {
  path: '/',
  component: layout,
  redirect: '/home',
  meta: {title: '首页', icon: 'el-icon-s-home'},
  children: [{
    path: '/home',
    // name: 'home',
    component: () => import('@/view/home/home'),
    meta: {title: '首页', icon: 'el-icon-s-home',requireAuth:true}
  }]
}
];
 export const doctorRouterMap = [
   {
     path: '/',
     component: layout,
     redirect: '/patientManagement',
     meta: {title: '预约管理', icon: 'el-icon-user'},
     children: [{
       path: '/appointDetail',
       component: () => import('@/view/patientManagement/appointDetail/appointDetail'),
       meta: {title: '预约详情', icon: 'el-icon-user',requireAuth:true}
     }]
   }, {
     path: '/',
     component: layout,
     redirect: '/outCallManagement',
     meta: {title: '出诊管理', icon: 'el-icon-s-cooperation'},
     children: [{
       path: '/outCallDetail',
       component: () => import('@/view/outCallManagement/outCallDetail'),
       meta: {title: '出诊详情', icon: 'el-icon-user',requireAuth:true}
     }]
   },
   {path: '*', redirect: '/404', hidden: true}
 ]

export const systemRouterMap = [
  {
    path: '/',
    component: layout,
    redirect: '/massageManagement',
    meta: {title: '店铺管理', icon: 'el-icon-office-building'},
    children: [{
      path: '/massageDetail',
      component: () => import('@/view/massageManagement/massageDetail'),
      meta: {title: '店铺详情', icon: 'el-icon-tickets',requireAuth:true}
    }, {
      path: '/checkMassage',
      component: () => import('@/view/massageManagement/checkMassage'),
      meta: {title: '查看店铺详情', icon: 'el-icon-user',requireAuth:true},
      hidden: true
    }, {
      path: '/addMassage',
      component: () => import('@/view/massageManagement/addMassage'),
      meta: {title: '添加店铺', icon: 'el-icon-user',requireAuth:true},
      hidden: true
    }, {
      path: '/editMassage',
      component: () => import('@/view/massageManagement/editMassage'),
      meta: {title: '修改店铺', icon: 'el-icon-user',requireAuth:true},
      hidden: true
    }]
   }
    , {
    path: '/',
    component: layout,
    redirect: '/doctorManagement',
    meta: {title: '师傅管理', icon: 'el-icon-s-custom'},
    children: [{
      path: '/doctorDetail',
      component: () => import('@/view/doctorManagement/doctorDetail'),
      meta: {title: '师傅详情', icon: 'el-icon-document',requireAuth:true}
    }, {
      path: '/checkDoctor',
      component: () => import('@/view/doctorManagement/checkDoctor'),
      meta: {title: '查看师傅', icon: 'el-icon-user',requireAuth:true},
      hidden: true
    }, {
      path: '/addDoctor',
      component: () => import('@/view/doctorManagement/addDoctor'),
      meta: {title: '添加师傅', icon: 'el-icon-user',requireAuth:true},
      hidden: true
    }, {
      path: '/editDoctor',
      component: () => import('@/view/doctorManagement/editDoctor'),
      meta: {title: '修改师傅', icon: 'el-icon-user',requireAuth:true},
      hidden: true
    }]
  },
  {
    path: '/',
    component: layout,
    redirect: '/setUpManagement',
    meta: {title: '出诊管理', icon: 'el-icon-document'},
    children: [
      {
        path: '/addVisitCall',
        component: () => import('@/view/setUpManagement/addVisitCall'),
        meta: {title: '添加出诊日期', icon: 'el-icon-date',requireAuth:true},
      }]
  },
  {
    path: '/',
    component: layout,
    redirect: '/patientManagement',
    meta: {title: '预约管理', icon: 'el-icon-s-order'},
    children: [{
      path: '/appointDetail',
      component: () => import('@/view/patientManagement/appointDetail/appointDetail'),
      meta: {title: '预约详情', icon: 'el-icon-s-claim',requireAuth:true}
    }
    ]
  },{
    path: '/',
    component: layout,
    redirect: '/productManagement',
    meta: {title: '产品管理', icon: 'el-icon-goods',requireAuth:true},
    children: [
      {
        path: '/appointProduct',
        component: () => import('@/view/productManagement/appointProduct/appointProduct'),
        meta: {title: '产品预约', icon: 'el-icon-s-claim',requireAuth:true},
      },
      {
      path: '/addProduct',
      component: () => import('@/view/productManagement/setUpProduct/addProduct'),
      meta: {title: '添加产品', icon: 'el-icon-circle-plus-outline',requireAuth:true},
      },
      {
      path: '/productCategory',
      component:() => import('@/view/productManagement/productCategory/productCategory'),
      meta: {title: '产品分类', icon: 'el-icon-folder-opened',requireAuth:true},
      }]
  },

  {
    path: '/',
    component: layout,
    redirect: '/wallpaper',
    meta: {title: '轮播图', icon: 'el-icon-picture-outline',requireAuth:true},
    children: [{
      path: '/wallpaperManagement',
      component: () => import('@/view/wallpaperManagement/wallpaper'),
      meta: {title: '轮播图管理', icon: 'el-icon-picture-outline',requireAuth:true}
    }]
  },
  {
    path: '/',
    component: layout,
    redirect: '/config',
    meta: {title: '配置', icon: 'el-icon-set-up',requireAuth:true},
    children: [{
      path: '/configManagement',
      component: () => import('@/view/config/isShowName'),
      meta: {title: '配置管理', icon: 'el-icon-set-up',requireAuth:true}
    }]
  },
  // {path: '*', redirect: '/404', hidden: true}
];

 const router = new VueRouter({
   mode:'hash',
   scrollBehavior: () => ({ y: 0 }),
   routes: constantRouterMap
 })
export default router;


//白名单
const whiteList = ['/']

// 配置顶部加载条
NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})

  // 路由前置守卫
  router.beforeEach((to, from, next) => {
    // 如果vuex不为空时添加路由
    ////console.log('store.getters',store.getters)
    // //console.log('store.getters.routers',store.getters.routers)
    // if(store.getters.routers.length !== 0){
    //   const accessRoutes = store.getters.routers;
    //   router.addRoutes(accessRoutes);
    // }
    //console.log('添加router当前用户',getCookie('username'))
    //console.log('添加router前',router.options)
    //判断当前用户是否是管理员,如果是管理员则添加管理员路由
    //console.log('1',getCookie('username') === 'admin' )
    //console.log('2',getCookie('username') !== '' && getCookie('username') !== 'admin')
    if (getCookie('username') === 'admin' ) {
      if (router.options.routes.length !== 10){
        let array = constantRouterMap.concat(systemRouterMap);
        router.options.routes = array;
        router.onReady(() => {
          router.addRoutes(array)
        });
      }
      // store.dispatch('SetRoutes', getCookie('username')).then(()=>{
      //   //console.log('vuex添加路由成功')
      // })
    }else if(getCookie('username') !== '' && getCookie('username') !== 'admin'){
      if (router.options.routes.length !== 6){
        let array = constantRouterMap.concat(doctorRouterMap);
        router.options.routes = array;
        router.addRoutes(array)
        //console.log('当前用户',getCookie('username'))
      }
      // store.dispatch('SetRoutes', getCookie('username')).then(()=>{
      //   //console.log('vuex添加路由成功')
      // })
    } else{
      //如果username为空则不操作
    }
    //console.log('添加router后',router.options)
    //console.log('进入路由前置守卫')
    //开启顶部加载效果
    NProgress.start()//开启加载效果
    // 判断meta里面的requireAuth true代表需要登录/权限
    //console.log('to',to)
    //console.log('from',from)
    if (to.meta.requireAuth) {
      //console.log("当前进入路由需要权限")
      // 判断本地是否存在token
      if (getToken() !== undefined){
        //console.log('存在token',getToken())
        next()
        // 这里是待会获取异步路由的地方
      } else {
        //console.log("未登录,重定向到登录界面")
        // 未登录,跳转到登陆页面
        next('/')
        NProgress.done()
      }
    } else {
      //白名单内的路由
      if (whiteList.indexOf(to.path) !== -1) {
        //console.log('进入白名单',getToken())
        next()
      } else {
        if (getToken() !== undefined ){
          //console.log('不在白名单，存在token')
          // 本地如果存在token，就跳转到/home
          next('/home')
        } else {
          //console.log('不在白名单，不存在token')
          // 本地不存在token，就跳转到/
          next('/')
          NProgress.done()
        }
      }
    }
  })


router.afterEach(() => {
  // 在即将进入新的页面组件前，关闭掉进度条
  NProgress.done()
})
